<?php

class usersActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {
        try {
            $this->users = UserRestResource::singleton()->findAll();

            // Getting user type descriptions
            $this->userTypes = UserTypeRestResource::singleton()->findAllAndMap('id', 'description');
        } catch (YougoException $e) {
            $this->getUser()->setFlashGlobalError();
        }
    }

    public function executeNew(sfWebRequest $request) {
        $this->form = new UserForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new UserForm();

        $this->processFormCreate($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        try {
            $user = UserRestResource::singleton()->find($request->getParameter('id'));
            $this->form = new UserForm($user);
        } catch (YougoException $e) {
            $this->getUser()->setFlashError();
            $this->redirect('users/index');
        }
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));

        try {
            $user = UserRestResource::singleton()->find($request->getParameter('id'));
            $this->form = new UserForm($user);
        } catch (YougoException $e) {
            $this->getUser()->setFlashError();
        }

        $this->processFormUpdate($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        try {
            UserRestResource::singleton()->delete($request->getParameter('id'));
            $this->form = new UserForm($user);
            $this->getUser()->setFlash('notice', 'User deleted successfully.');
        } catch (YougoException $e) {
            $this->getUser()->setFlashError();
        }

        $this->redirect('users/index');
    }

    protected function processFormCreate(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            try {
                $id = UserRestResource::singleton()->create($form->getValues());
                $user = UserRestResource::singleton()->find($id);
                $this->getUser()->setFlash('notice', 'User created successfully.');
                $this->redirect('users/edit?id=' . $user->getId());
            } catch (YougoException $e) {
                $this->getUser()->setFlashError();
            }
        }
    }

    protected function processFormUpdate(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            try {
                UserRestResource::singleton()->update($form->getValues());
                $user = UserRestResource::singleton()->find($form->getValue('id'));
                $this->getUser()->setFlash('notice', 'User updated successfully.');
                $this->redirect('users/edit?id=' . $user->getId());
            } catch (YougoException $e) {
                $this->getUser()->setFlashError();
            }
        }
    }

}
